package com.nl.controller.sms5g.controller;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.global.GlobalConstants;
import com.nl.controller.common.interceptor.SysContext;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.BaseResult;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.sms5g.bean.bo.CreateSignatureBo;
import com.nl.controller.sms5g.bean.bo.QrySignatureBo;
import com.nl.controller.sms5g.bean.bo.QrySignatureBo2;
import com.nl.controller.sms5g.bean.bo.UpdateSignatureBo;
import com.nl.controller.sms5g.bean.po.SignatureInfo;
import com.nl.controller.sms5g.service.Sms5gSignatureService;
import com.nl.controller.web.bean.BlackUser;
import com.nl.controller.web.bean.SmsTemplateAttr;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.web.util.FileUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 5G多媒体签名
 * @return null
 * @author l
 * @date 2021/5/27 10:04
 */
@Validated
@RestController
@Slf4j
@RequestMapping("/signatureManage")
public class Sms5gSignatureController {
    @Autowired
    private Sms5gSignatureService sms5gSignatureService;

    /**
     * @desc 新建5G多媒体签名
     * @param file
     * @param request
     * @param signName
     * @param ecName
     * @param cityId
     * @param countyId
     * @param industryType
     * @param customerType
         * @return com.nl.controller.common.util.BaseResult
     * @author l
     * @date 2021/5/30 21:15
     */
    @RequestMapping("/createSignature")
    public BaseResult createSignature(
            @RequestParam("file") MultipartFile file,
            HttpServletRequest request,
            @RequestParam(value = "signName", required = true) String signName,
            @RequestParam(value = "ecName", required = true) String ecName,
            @RequestParam(value = "cityId", required = true) String cityId,
            @RequestParam(value = "countyId", required = true) String countyId,
            @RequestParam(value = "industryType", required = true) String industryType,
            @RequestParam(value = "customerType", required = true) String customerType)
            {
        HttpSession session = SysContext.getRequest().getSession();
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String userId = user.getUserId();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHH24mmss");
        String fileName = sdf.format(new Date());
        fileName = fileName+".png";
        String path = GlobalConst.SIGNNATURE_FILE_PATH;

        CreateSignatureBo createSignatureBo = new CreateSignatureBo();
        createSignatureBo.setSignName(signName);
        createSignatureBo.setEcName(ecName);
        createSignatureBo.setCityId(cityId);
        createSignatureBo.setCountyId(countyId);
        createSignatureBo.setIndustryType(industryType);
        createSignatureBo.setCustomerType(customerType);
        createSignatureBo.setUserId(userId);
        createSignatureBo.setAuthorizationId(fileName);
        createSignatureBo.setHasAttachment("1");
        //签名授权文件上传主机目录
        FileUtil.fileUpload(file, request, path, fileName);

        return sms5gSignatureService.createSignature(createSignatureBo);
    }

    @RequestMapping("/createSignature2")
    public BaseResult createSignature(
            @RequestParam(value = "signName", required = true) String signName,
            @RequestParam(value = "ecName", required = true) String ecName,
            @RequestParam(value = "cityId", required = true) String cityId,
            @RequestParam(value = "countyId", required = true) String countyId,
            @RequestParam(value = "industryType", required = true) String industryType,
            @RequestParam(value = "customerType", required = true) String customerType)
    {
        HttpSession session = SysContext.getRequest().getSession();
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String userId = user.getUserId();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHH24mmss");
        CreateSignatureBo createSignatureBo = new CreateSignatureBo();
        createSignatureBo.setSignName(signName);
        createSignatureBo.setEcName(ecName);
        createSignatureBo.setCityId(cityId);
        createSignatureBo.setCountyId(countyId);
        createSignatureBo.setIndustryType(industryType);
        createSignatureBo.setCustomerType(customerType);
        createSignatureBo.setUserId(userId);
        createSignatureBo.setHasAttachment("0");
        return sms5gSignatureService.createSignature(createSignatureBo);
    }
    
    /**
     * @desc 变更签名信息
     * @param updateSignatureBo 
     * @return com.nl.controller.common.util.BaseResult
     * @author l
     * @date 2021/6/1 17:30
     */
    @RequestMapping("/updateSignature")
    public BaseResult updateSignature(@RequestBody @Validated UpdateSignatureBo updateSignatureBo){
        BaseResult baseResult = sms5gSignatureService.updateSignature(updateSignatureBo);
        return baseResult;
    }

    /**
     * @desc 查询签名列表（有分页）
     * @param
     * @return com.nl.controller.common.util.BaseResult<com.nl.controller.sms5g.bean.po.SignatureInfo>
     * @author l
     * @date 2021/6/2 16:33
     */
    @RequestMapping("/qrySignatureList")
    public Response qrySignatureList(@RequestParam(value = "page", required = true) int page,
                                     @RequestParam(value = "limit", required = true) int limit,
                                     @RequestParam(value = "cityId",required = false) String cityId,
                                     @RequestParam(value = "countyId",required = false) String countyId,
                                     @RequestParam(value = "signName",required = false) String signName,
                                     @RequestParam(value = "ecName",required = false) String ecName,
                                     @RequestParam(value = "industryType",required = false) String industryType,
                                     @RequestParam(value = "checkState",required = false) String checkState,
                                     @RequestParam(value = "customerType",required = false) String customerType,
                                     @RequestParam Map<String, Object> paramMap){
        log.info("查询签名列表（有分页）请求参数："+paramMap.toString());
        PageInfo<SignatureInfo> pageInfo = sms5gSignatureService.qrySignatureList(page,limit,cityId,countyId,signName,ecName,checkState,industryType,customerType);
        log.info("查询签名列表（有分页）返回参数："+pageInfo.toString());
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal(),pageInfo.getPages());
    }

    /**
     * @desc 查询签名列表（无分页）
     * @param qrySignatureBo 
     * @return com.nl.controller.common.util.BaseResult<java.util.List<com.nl.controller.sms5g.bean.po.SignatureInfo>>
     * @author l
     * @date 2021/6/18 14:29
     */
    @RequestMapping("/qrySignatureList2")
    public BaseResult<List<SignatureInfo>> qrySignatureList2(@RequestBody QrySignatureBo2 qrySignatureBo){
        BaseResult<List<SignatureInfo>> baseResult = sms5gSignatureService.qrySignatureList2(qrySignatureBo);
        return baseResult;
    }
    
    /**
     * @desc 判断签名名称是否重复
     * @param signName 
     * @return java.lang.Boolean
     * @author l
     * @date 2021/6/17 11:13
     */
    @RequestMapping("/judgeSignNameRepeat")
    public BaseResult<Integer> judgeSignNameRepeat(@RequestParam(value = "signName", required = true)String signName){
        return sms5gSignatureService.judgeRepeat(signName);
    }

    /**
     * @desc 签名授权文件下载
     * @param res
     * @return void
     * @author l
     * @date 2021/6/21 14:18
     */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(HttpServletResponse res) {
        String fileName = "sign.pdf";
        FileUtil.download(fileName, GlobalConst.SIGNNATURE_FILE_PATH, res);
    }

    @PostMapping("remove")
    public int removeSign(@RequestParam(value = "signId", required = true) String signId
    ) {
        int result = sms5gSignatureService.removeSign(signId);
        log.info("签名删除请求参数:"+signId);
        log.debug("签名删除返回数据：{}", result);
        return result;
    }

    @PostMapping("reApply")
    public BaseResult reApplySign(@RequestParam(value = "signId", required = true) String signId
    ) {
        BaseResult result = sms5gSignatureService.reApplySign(signId);
        log.info("签名删除请求参数:"+signId);
        log.debug("签名删除返回数据：{}", result);
        return result;
    }



}
